<?php

namespace App\Http\Controllers\Home;
use Hash;
use DB;
use Illuminate\Http\Request;
use App\Http\Requests\UserInsertRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;

class LoginController extends Controller
{
   
      //注册用户方法
      public function getRegist()
      {
            //解析注册模板
            return view('home.user.zhu');
          
      } 

      public function postZhuce(Request $request)
      {

             //判断验证码
             if($request->input('vcode') != session('Vcode')){
                return back()->with('error','验证码错误');
             }
             
             //获取用户注册信息
             $zhuce = $request->only(['uname','tel','upwd','email','_token']);
            
             //判断用户名是否存在
             $hu = $zhuce['uname'];
             // dd($hu);
             $res = DB::table('shop_users')->where('uname',$hu)->first();
             // dd($res);
             if(!empty($res)){
                   return back()->with('error','请换用户名');
             }

             //判断用户名 手机号 密码是否为空
             if(!$zhuce['uname'] || !$zhuce['tel'] || !$zhuce['upwd'] ){
                  return back();
             }
             
             $zhu = $request->only(['uname','tel','email','_token']);

             $zhu['upwd'] =  Hash::make($zhuce['upwd']);

             //信息插入数据库
             $res = DB::table('shop_users')->insert($zhu);
             
             if($res){
              return redirect('/Home/login')->with('success','恭喜你注册成功');
              }else{
                    return back()->with('error','请再注册哦');
              }
             // dd($zhuce);
             
            
      }


      //用户登录
      public function getLogin()
      {
            //解析注册模板
            return view('home.user.index');
             
      }

      public function postLayout(Request $request)
      {
           //判断用户名密码
           $uname = $request->input('uname');
           $upwd = $request->input('upwd');
                 // dd($uname);
           $res = DB::table('shop_users')->where('uname',$uname)->first();
           // dd($res);
           //存入用户id到session中
           
           //判断登录信息
           if(!empty($res) && $upwd=$res->upwd){
           $request->session()->put('uid',$res->uid);
              return view('welcome');
           }else{
              return back()->with('error','登录名或密码不正确');
           }

      }

      //忘记密码
      public function getPassinfo()
      {

           //解析模板
           return view('Home.user.pass');
      }

      

      //接受ajax请求 发送短信验证
      public function SendCode(Request $request)
      {    
            $phone = $request->input('phone');
            // dd($phone);
            //初始化必填
            $options['accountsid']='6d692771dc33132278adfcff3759f229';
            $options['token']='1f5dfee0b181d5ae45c5fed34d82c6c7';

            // //初始化 $options必填
            $ucpass = new \Ucpaas($options);

            // echo $ucpass->getDevinfo('xml');

            $yan = rand(11111,99999);
            //存入用户id到session中
            $request->session()->put('mobile_code', $yan);

            // //语音验证码,云之讯融合通讯开放平台收到请求后，向对象电话终端发起呼叫，接通电话后将播放指定语音验证码序列
            // $appId = "e351246607524ec69029799b60e1c69d";
            // $verifyCode = rand(11111,99999);
            // $to = $phone;

            //存入用户手机验证码到session中
            // $request->session()->put('mobile_code', $request->input('mobile_code'));

            // echo $ucpass->voiceCode($appId,$verifyCode,$to);

            //短信验证码（模板短信）,默认以65个汉字（同65个英文）为一条（可容纳字数受您应用名称占用字符影响），超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
            // dd($phone);
            $appId = "e351246607524ec69029799b60e1c69d";
            $to = $phone;
            $templateId = '35426';//短信模板id号
            $param="戴瑞珠宝官网,$yan,60";
            // dd($param);
            echo $ucpass->templateSMS($appId,$to,$templateId,$param);

      }
      
      //接受用户重置密码提交过来的信息
      public function postMima(Request $request)
      {
             $chong = $request->only(['tel','upwd','password','mobile_code']);
             
             //通过手机号查看数据库里存在这个用户吗
             $res = DB::table('shop_users')->where('tel',$chong['tel'])->first();
             if(empty($res)){
                  return back()->with('error','用户不存在');
             }
             //判断用户名 手机号 密码是否为空
             if(!$chong['tel'] || !$chong['upwd'] || !$chong['mobile_code'] ){
                  return back();
             }

             //判断验证码
             if($request->input('mobile_code') != session('mobile_code')){
                  return back()->with('error','验证码错误');
             }
             //判断密码是否一样
             if($chong['upwd'] == $chong['password']){
                  //信息插入数据库
                  $mi = $request->only(['upwd']);
                  
                  $mi['upwd'] = Hash::make($mi['upwd']);
                  // dd($mi);
                  $res = DB::table('shop_users')->where('uid',session('uid'))->update($mi);
                  //判断是否修改成功
                  if($res){
              
                         return redirect('/Home/login')->with('success','密码修改成功');
                  }else{
                         return back()->with('error','密码修改失败');
                  }
                  
            }else{
                  return back();
             }
      }
             

      //验证码
      public function Vcode()

      {
          ob_clean();//清除输出缓存区的内容
          //生成验证码图片的Builder对象，配置相应属性
          $builder = new CaptchaBuilder;
          //可以设置图片宽高及字体
          $builder->build($width = 100, $height = 40, $font = null);
          //获取验证码的内容
          $phrase = $builder->getPhrase();

          //把内容存入session
          session(['Vcode'=>$phrase]);
          //生成图片
          header("Cache-Control: no-cache, must-revalidate");
          header('Content-Type: image/jpeg');
          $builder->output();
           // die;  
           // 不查看验证码的时候注视了
      }

}